import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/e_tab.dart';
import 'package:boss/ui/widget/tab_bar_decoration.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

import 'custom_tab_bar_view.dart';

class CustomTab extends StatefulWidget {
  final List<dynamic> tabs;
  final List<Widget> tabViews;
  final EdgeInsets padding;
  final bool isScrollable;
  final int initialIndex;
  final ValueChanged<int> onChanged;

  const CustomTab({
    Key key,
    this.tabs,
    this.tabViews,
    this.padding = EdgeInsets.zero,
    this.isScrollable = true,
    this.initialIndex = 0,
    this.onChanged,
  })  : assert(tabs.length == tabViews.length),
        super(key: key);

  @override
  _CustomTabState createState() => _CustomTabState();
}

class _CustomTabState extends State<CustomTab> with SingleTickerProviderStateMixin {
  TabController _controller;

  @override
  void initState() {
    super.initState();

    _controller = TabController(
      initialIndex: widget.initialIndex,
      length: widget.tabs.length,
      vsync: this,
    );

    _controller.addListener(_handleEvent);
  }

  _handleEvent() {
    if (widget.onChanged != null) {
      widget.onChanged(_controller.index);
    }
  }

  @override
  void dispose() {
    _controller.removeListener(_handleEvent);
    _controller.dispose();
    _controller = null;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Theme(
      data: ThemeData(
        tabBarTheme: TabBarTheme(
          labelStyle:  TextStyle(
            fontSize: 34.sp,
            color: ColorConstant.ff33,
            fontWeight: FontWeight.w500,
          ),
          unselectedLabelStyle: TextStyleConstant().normal_2_28,
          labelColor: ColorConstant.ff33,
          indicatorSize: TabBarIndicatorSize.label,
          indicator: MUnderlineTabIndicator(),
          labelPadding: EdgeInsets.symmetric(
            horizontal: 20.w,
            vertical: 0.w,
          ),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: widget.padding,
            child: ETabBar(
              controller: _controller,
              isScrollable: widget.isScrollable,
              tabs: widget.tabs[0] is String ? widget.tabs.map((tab) => ETab(text: tab)).toList() : widget.tabs,
            ),
          ),
          Expanded(
            child: CustomTabBarView(
              controller: _controller,
              children: widget.tabViews,
            ),
          ),
        ],
      ),
    );
  }
}
